package com.ld.xuanKe.service.impl;

import com.ld.xuanKe.dao.CourseDAO;
import com.ld.xuanKe.dao.SelectdDAO;
import com.ld.xuanKe.entity.Course;
import com.ld.xuanKe.entity.Selectd;
import com.ld.xuanKe.service.CourseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName xuanKe
 * @ClassName CourseServiceImpl
 * @Author 刘邓
 * @Date 2023-12-09 10:42
 */
@Service
public class CourseServiceImpl implements CourseService {
	@Resource
	private CourseDAO courseDAO;
	@Resource
	private SelectdDAO selectdDAO;

	public int update(Course course) {
		return courseDAO.update(course);
	}

	public int add(Course course) {
		return courseDAO.insert(course);
	}

	public int delete(Integer id) {
		try {
			List<Selectd> selectds = selectdDAO.find("from Selectd S where S.courseId = ?", new Object[]{id});
			for (Selectd selectd : selectds) {
				selectdDAO.delete(selectd);
			}
			courseDAO.delete(Course.class, id);
		} catch (Exception e) {
			System.out.println(e);
			return 0;
		}
		return 1;
	}

	public List<Course> list() {
		return courseDAO.find("from Course", new Object[]{});
	}

	public Course querySingle(Integer id) {
		return courseDAO.findOne("from Course c where c.id = ?", new Object[]{id});
	}

	public int updateTime(Timestamp startTime, Timestamp endTime) {
		int fromCourse = courseDAO.getCount("select count(*) from Course", new Object[]{});
		int i = courseDAO.updateList("update Course c set c.startTime = ?,c.endTime = ?", new Object[]{startTime, endTime});
		if (fromCourse == i) {
			return 1;
		}
		return 0;
	}

	public List<Course> getCoursesByLikeName(String name) {
		return courseDAO.find("from Course C where C.name like ?", new Object[]{"%" + name + "%"});
	}

	public List<Course> getValidCourses() {
		Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now());
		return courseDAO.find("from Course C where (C.startTime is null and C.endTime is not null and C.endTime >= ?)" +
				"or(C.startTime is not null and C.endTime is null and C.startTime <= ?)"
				+ "or(C.startTime is not null and C.endTime is not null and C.startTime <= ? and C.endTime >= ?)", new Object[]{timestamp, timestamp, timestamp, timestamp});
	}

	public int deleteNotValid() {
		Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now());
//		查找course表中不符合条件的课程id
		List<Course> courses = courseDAO.find("from Course C where C.capacity < 40 and ((C.startTime is null and C.endTime is not null and C.endTime < ?)"
				+ "or(C.startTime is not null and C.endTime is not null and C.endTime < ?))", new Object[]{timestamp, timestamp});
		List<Integer> courseIds = new ArrayList<Integer>();
		if (!courses.isEmpty()) {
//			删除选课表中选择的不符合课程的选课信息
			for (Course course : courses) {
				courseIds.add(course.getId());
			}
			try {
				for (Integer courseId : courseIds) {
					List<Selectd> selectds = selectdDAO.find("from Selectd S where S.courseId = ?", new Object[]{courseId});
					for (Selectd selectd : selectds) {
						selectdDAO.delete(selectd);
					}
				}
			} catch (Exception e) {
				System.out.println(e);
				return 0;
			}
			courseDAO.deleteList(Course.class, courseIds);
		}
		return 1;
	}

	public List<Course> getSelectCourse(Integer userId) {
		List<Course> courses = null;
		List<Selectd> selectds = selectdDAO.find("from Selectd where userId = ?", new Object[]{userId});
		courses = getValidCourses();
		System.out.println(courses);
		List<Course> newCourses = new ArrayList<Course>();
//		查询用户选过的课程
		for (Selectd selectd : selectds) {
			for (Course course : courses) {
				if (selectd.getCourseId() == course.getId()) {
					newCourses.add(course);
				}
			}
		}
		System.out.println("用户选的课" + newCourses);
//删除用户选过的课程
		List<Integer> ids = new ArrayList<Integer>();
		for (Course course1 : newCourses) {
			for (Course course : courses) {
				if (course.getId() == course1.getId()) {
					ids.add(course.getId());
				}
			}
		}
		System.out.println("待删除的课程id" + ids);
		for (Integer id : ids) {
		for (int i = 0; i < courses.size(); i++) {
				if (courses.get(i).getId() == id) {
					courses.remove(courses.get(i));
					i = 0;
				}
		}
		}
		System.out.println(courses);
		return courses;
	}

	public List<Course> getSelectCourseByLikeName(Integer userId,String courseName) {
		List<Course> selectCourse = getSelectCourse(userId);
		List<Course> courses = new ArrayList<Course>();
		for (Course course : selectCourse) {
			int i = course.getName().indexOf(courseName);
			if(i != -1){
				courses.add(course);
			}
		}
		return courses;
	}

	public List<Course> getValidCoursesAfterSelect() {
		Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now());
//		查找course表中符合条件的课程id
		List<Course> courses = courseDAO.find("from Course C where C.capacity >= 40 and ((C.startTime is null and C.endTime is not null and C.endTime < ?)"
				+ "or(C.startTime is not null and C.endTime is not null and C.endTime < ?))", new Object[]{timestamp, timestamp});
		return courses;
	}
}
